using UnityEditor;
using System.Collections.Generic;
using System.Reflection;
using UnityEditorInternal;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(ProjectConfiguration))]
    public class ProjectConfigEditor : UnityEditor.Editor
    {
        List<string> mAllConfigs = new List<string>();
        ReorderableList mList;

        private void OnEnable()
        {
            mAllConfigs.Clear();
            var cfgs = serializedObject.FindProperty("m_AllConfigs");
            for (int i = 0; i < cfgs.arraySize; i++)
            {
                var p = cfgs.GetArrayElementAtIndex(i);
                var inst = p.managedReferenceValue as ProjectConfiguration.Configurable;
                if (inst == null)
                {
                    mAllConfigs.Add("<missing configuration>");
                }
                else
                {
                    var type = inst.GetType();
                    var title = inst.GetTitle(type.GetCustomAttribute<ProjectConfigAttribute>());
                    mAllConfigs.Add($"{title} ({type.Name})");
                }
            }
            if (mList == null)
                mList = new ReorderableList(mAllConfigs, typeof(string), false, false, false, false);
        }

        public override void OnInspectorGUI()
        {
            mList.DoLayoutList();
        }
    }
}
